/*
 * ==========================================================================*\
 * | $Id$
 * |*-------------------------------------------------------------------------*|
 * | Copyright (C) 2009 Virginia Tech | | This file is part of CloudSpace. | |
 * CloudSpace is free software; you can redistribute it and/or modify | it under
 * the terms of the GNU General Public License as published | by the Free
 * Software Foundation; either version 3 of the License, or | (at your option)
 * any later version. | | CloudSpace is distributed in the hope that it will be
 * useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of |
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General
 * Public License for more details. | | You should have received a copy of the
 * GNU General Public License | along with CloudSpace; if not, see
 * <http://www.gnu.org/licenses/>.
 * \*==========================================================================
 */

package cloudspace.security.permissions;

import cloudspace.security.CloudSpaceUser;


// -------------------------------------------------------------------------
/**
 * Represents a directory permission token that is controlled based on a group.
 * 
 * @author Tony Allevato
 * @author Last changed by $Author$
 * @version $Revision$, $Date$
 */
public class UserPermissionToken extends PermissionToken
{
    // ~ Constructors ..........................................................

    // ----------------------------------------------------------
    public UserPermissionToken(
        String username,
        boolean allowsRead,
        boolean allowsWrite )
    {
        super( allowsRead, allowsWrite );

        this.username = username;
    }


    // ----------------------------------------------------------
    public UserPermissionToken( String username, String permissions )
    {
        super( permissions );

        this.username = username;
    }


    // ~ Methods ...............................................................

    // ----------------------------------------------------------
    public String getUsername()
    {
        return username;
    }


    // ----------------------------------------------------------
    @Override
    public boolean matchesUser( CloudSpaceUser user )
    {
        return user.getUsername().equals( username );
    }


    // ----------------------------------------------------------
    @Override
    public boolean isSimilarTo( PermissionToken token )
    {
        if ( token instanceof UserPermissionToken )
        {
            UserPermissionToken uToken = (UserPermissionToken)token;
            return uToken.getUsername().equals( getUsername() );
        }
        else
        {
            return false;
        }
    }


    // ----------------------------------------------------------
    @Override
    public int getSpecificity()
    {
        return 1000;
    }


    // ----------------------------------------------------------
    @Override
    public String toString()
    {
        StringBuffer buffer = new StringBuffer();
        buffer.append( username );
        buffer.append( " = " );

        if ( allowsRead() )
        {
            buffer.append( 'r' );
        }

        if ( allowsWrite() )
        {
            buffer.append( 'w' );
        }

        return buffer.toString();
    }

    // ~ Static/instance variables .............................................

    private String username;
}
